<?php

namespace App\Providers;

use App\Common\Common;
use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(255); //add fixed sql
        $this->addRule();
        $this->QueryAddFunc();
        DB::connection()->enableQueryLog();;
    }


    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * 新增自定义验证规则
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function addRule()
    {
        Validator::extend('phone', function ($attribute, $value, $parameters, $validator) {

            return preg_match('/^1[3456789][0-9]{9}$/', $value) > 0 ? true : false;
        });

        Validator::extend('code', function ($attribute, $value, $parameters, $validator) {

            return preg_match('/^[0-9]{6}$/', $value) > 0 ? true : false;
        });



    }

    /**
     * 增加方法
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function QueryAddFunc()
    {
        Builder::macro('cache', function (...$params) {

            return $this->getConnection()->cache(...$params);

        });
    }



}
